package com.lk.recvice.mq.receive.handler.datas;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 根据队列类型区分数据类型
 * @author Admin
 *
 */
@Component
@RabbitListener(queues = "ReportDatas_DirectQueue_Modbus_Client")//监听的队列名称 TestDirectQueue
public class ReportDataDirectReceiver {

	@Autowired
	private ReportDataReceiveHandler reportDataReceiveHandler;

	@RabbitHandler
    public void receive(String context,Channel channel, Message message) throws IOException  {
        System.out.println("server datas Receive:"+ message);

        try {
        	//处理消息
        	reportDataReceiveHandler.receiveHandler(message, context);
        	// 应答模式确认接收到消息,后面那个参数如果是false，就是只确认签收当前消息，如果是true，则签收全部比当前TagId小的消息

			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (IOException e) {
			//丢掉
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
			//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
			e.printStackTrace();
		}

    }
}
